﻿<?php require_once('../Connections/hitekforyou.php'); ?>
<?php
require_once('checklogin.php');
//MX Widgets3 include
require_once('../includes/wdg/WDG.php');

error_reporting(0);
// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Load the KT_back class
require_once('../includes/nxt/KT_back.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_hitekforyou = new KT_connection($hitekforyou, $database_hitekforyou);



// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("idSP", true, "numeric", "", "", "", "Vui lòng chọn sản phẩm khuyến mãi");
$formValidation->addField("NoiDungKM", true, "text", "", "", "", "Vui lòng điền đúng dữ liệu cần");
$formValidation->addField("TGBD", true, "date", "", "", "", "Vui lòng điền đúng dữ liệu cần");
$formValidation->addField("TGKT", true, "date", "", "", "", "Vui lòng điền đúng dữ liệu cần");
$tNGs->prepareValidation($formValidation);
// End trigger

//start Trigger_FileDelete trigger
//remove this line if you want to edit the code by hand 
function Trigger_FileDelete(&$tNG) {
  $deleteObj = new tNG_FileDelete($tNG);
  $deleteObj->setFolder("../UploadImage/KhuyenMai/");
  $deleteObj->setDbFieldName("HinhAnh");
  return $deleteObj->Execute();
}
//end Trigger_FileDelete trigger

//start Trigger_ImageUpload trigger
//remove this line if you want to edit the code by hand 
function Trigger_ImageUpload(&$tNG) {
  $uploadObj = new tNG_ImageUpload($tNG);
  $uploadObj->setFormFieldName("HinhAnh");
  $uploadObj->setDbFieldName("HinhAnh");
  $uploadObj->setFolder("../UploadImage/KhuyenMai/");
  $uploadObj->setResize("false", 560, 400);
  $uploadObj->setMaxSize(2000);
  $uploadObj->setAllowedExtensions("gif, jpg, jpe, jpeg, png, swf");
  $uploadObj->setRename("auto");
  return $uploadObj->Execute();
}
//end Trigger_ImageUpload trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_hitekforyou, $hitekforyou);
$query_Recordset1 = "SELECT TenKH, idKH FROM khachhang ORDER BY TenKH";
$Recordset1 = mysql_query($query_Recordset1, $hitekforyou) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_hitekforyou, $hitekforyou);
$query_Recordset2 = "SELECT idSP, idSP FROM sanpham ORDER BY idSP";
$Recordset2 = mysql_query($query_Recordset2, $hitekforyou) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

// Make a logout transaction instance
$logoutTransaction = new tNG_logoutTransaction($conn_hitekforyou);
$tNGs->addTransaction($logoutTransaction);
// Register triggers
$logoutTransaction->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "GET", "KT_logout_now");
$logoutTransaction->registerTrigger("END", "Trigger_Default_Redirect", 99, "login.php");
// Add columns
// End of logout transaction instance

// Make an insert transaction instance
$ins_khuyenmai = new tNG_multipleInsert($conn_hitekforyou);
$tNGs->addTransaction($ins_khuyenmai);
// Register triggers
$ins_khuyenmai->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_khuyenmai->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_khuyenmai->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
$ins_khuyenmai->registerTrigger("AFTER", "Trigger_ImageUpload", 97);
// Add columns
$ins_khuyenmai->setTable("khuyenmai");
$ins_khuyenmai->addColumn("idSP", "NUMERIC_TYPE", "POST", "idSP");
$ins_khuyenmai->addColumn("HinhAnh", "FILE_TYPE", "FILES", "HinhAnh");
$ins_khuyenmai->addColumn("NoiDungKM", "STRING_TYPE", "POST", "NoiDungKM");
$ins_khuyenmai->addColumn("TGBD", "DATE_TYPE", "POST", "TGBD");
$ins_khuyenmai->addColumn("TGKT", "DATE_TYPE", "POST", "TGKT");
$ins_khuyenmai->setPrimaryKey("idKM", "NUMERIC_TYPE");

// Make an update transaction instance
$upd_khuyenmai = new tNG_multipleUpdate($conn_hitekforyou);
$tNGs->addTransaction($upd_khuyenmai);
// Register triggers
$upd_khuyenmai->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$upd_khuyenmai->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_khuyenmai->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
$upd_khuyenmai->registerTrigger("AFTER", "Trigger_ImageUpload", 97);
// Add columns
$upd_khuyenmai->setTable("khuyenmai");
$upd_khuyenmai->addColumn("idSP", "NUMERIC_TYPE", "POST", "idSP");
$upd_khuyenmai->addColumn("HinhAnh", "FILE_TYPE", "FILES", "HinhAnh");
$upd_khuyenmai->addColumn("NoiDungKM", "STRING_TYPE", "POST", "NoiDungKM");
$upd_khuyenmai->addColumn("TGBD", "DATE_TYPE", "POST", "TGBD");
$upd_khuyenmai->addColumn("TGKT", "DATE_TYPE", "POST", "TGKT");
$upd_khuyenmai->setPrimaryKey("idKM", "NUMERIC_TYPE", "GET", "idKM");

// Make an instance of the transaction object
$del_khuyenmai = new tNG_multipleDelete($conn_hitekforyou);
$tNGs->addTransaction($del_khuyenmai);
// Register triggers
$del_khuyenmai->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Delete1");
$del_khuyenmai->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
$del_khuyenmai->registerTrigger("AFTER", "Trigger_FileDelete", 98);
// Add columns
$del_khuyenmai->setTable("khuyenmai");
$del_khuyenmai->setPrimaryKey("idKM", "NUMERIC_TYPE", "GET", "idKM");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rscustom = $tNGs->getRecordset("custom");
$row_rscustom = mysql_fetch_assoc($rscustom);
$totalRows_rscustom = mysql_num_rows($rscustom);

// Get the transaction recordset
$rskhuyenmai = $tNGs->getRecordset("khuyenmai");
$row_rskhuyenmai = mysql_fetch_assoc($rskhuyenmai);
$totalRows_rskhuyenmai = mysql_num_rows($rskhuyenmai);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wdg="http://ns.adobe.com/addt">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="quantri.css" rel="stylesheet" type="text/css" />
<script src="../AC_RunActiveContent.js" type="text/javascript"></script>

<title>Quản trị web HitekForYou</title>
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../includes/skins/style.js" type="text/javascript"></script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style><?php echo $tNGs->displayValidationRules();?><script src="../includes/nxt/scripts/form.js" type="text/javascript"></script><script src="../includes/nxt/scripts/form.js.php" type="text/javascript"></script><script type="text/javascript">
$NXT_FORM_SETTINGS = {
  duplicate_buttons: true,
  show_as_grid: true,
  merge_down_value: true
}
</script>
<script type="text/javascript" src="../includes/common/js/sigslot_core.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js.php"></script>
<script type="text/javascript" src="../includes/wdg/classes/Calendar.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/SmartDate.js"></script>
<script type="text/javascript" src="../includes/wdg/calendar/calendar_stripped.js"></script>
<script type="text/javascript" src="../includes/wdg/calendar/calendar-setup_stripped.js"></script>
<script src="../includes/resources/calendar.js"></script>
</head>

<body>

<?php include("admin_template/header.php"); ?>
<?php include("admin_template/admin_banner.php"); ?> 
<table id="main" width="990" border="1" align="center" cellpadding="0" cellspacing="0">
 <tr>
    <td width="200" align="left" valign="top" bgcolor="#66CC00" id="cottrai">
      <div align="left">
      <?php include("menu.php"); ?>  
      </div>       </td>
    <td id="cotphai" width="790" align="left" valign="top" bgcolor="#FFFFFF">
    
<?php
	echo $tNGs->getErrorMsg();
?>
<div class="KT_tng">
  <h1>
    <?php 
// Show IF Conditional region1 
if (@$_GET['idKM'] == "") {
?>
    Thêm
    <?php 
// else Conditional region1
} else { ?>
    Cập nhật
    <?php } 
// endif Conditional region1
?>
    Khuyến mãi </h1>
  <div class="KT_tngform">
    <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>" enctype="multipart/form-data">
      <?php $cnt1 = 0; ?>
      <?php do { ?>
      <?php $cnt1++; ?>
      <?php 
// Show IF Conditional region1 
if (@$totalRows_rskhuyenmai > 1) {
?>
      <h2><?php echo NXT_getResource("Record_FH"); ?> <?php echo $cnt1; ?></h2>
      <?php } 
// endif Conditional region1
?>
      <table cellpadding="2" cellspacing="0" class="KT_tngtable">
        <tr>
          <td class="KT_th"><label for="idSP_<?php echo $cnt1; ?>">ID Sản phẩm:</label></td>
          <td><select name="idSP_<?php echo $cnt1; ?>" id="idSP_<?php echo $cnt1; ?>">

            <?php 
do {  
?>
            <option value="<?php echo $row_Recordset2['idSP']?>"<?php if (!(strcmp($row_Recordset2['idSP'], $row_rskhuyenmai['idSP']))) {echo "SELECTED";} ?>><?php echo $row_Recordset2['idSP']?></option>
            <?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
  $rows = mysql_num_rows($Recordset2);
  if($rows > 0) {
      mysql_data_seek($Recordset2, 0);
	  $row_Recordset2 = mysql_fetch_assoc($Recordset2);
  }
?>
          </select>
              <?php echo $tNGs->displayFieldError("khuyenmai", "idSP", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="HinhAnh_<?php echo $cnt1; ?>">Hình ảnh:</label></td>
          <td><input type="file" name="HinhAnh_<?php echo $cnt1; ?>" id="HinhAnh_<?php echo $cnt1; ?>" size="32" />
              <?php echo $tNGs->displayFieldError("khuyenmai", "HinhAnh", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="NoiDungKM_<?php echo $cnt1; ?>">Nội dung khuyến mãi:</label></td>
          <td><textarea name="NoiDungKM_<?php echo $cnt1; ?>" id="NoiDungKM_<?php echo $cnt1; ?>" cols="50" rows="5"><?php echo KT_escapeAttribute($row_rskhuyenmai['NoiDungKM']); ?></textarea>
              <?php echo $tNGs->displayFieldHint("NoiDungKM");?> <?php echo $tNGs->displayFieldError("khuyenmai", "NoiDungKM", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="TGBD_<?php echo $cnt1; ?>">Thời gian bắt đầu:</label></td>
          <td><input name="TGBD_<?php echo $cnt1; ?>" id="TGBD_<?php echo $cnt1; ?>" value="<?php echo KT_formatDate($row_rskhuyenmai['TGBD']); ?>" size="10" maxlength="22" wdg:mondayfirst="false" wdg:subtype="Calendar" wdg:mask="<?php echo $KT_screen_date_format; ?>" wdg:type="widget" wdg:singleclick="false" wdg:restricttomask="no" wdg:readonly="true" />
              <?php echo $tNGs->displayFieldHint("TGBD");?> <?php echo $tNGs->displayFieldError("khuyenmai", "TGBD", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="TGKT_<?php echo $cnt1; ?>">Thời gian kết thúc:</label></td>
          <td><input name="TGKT_<?php echo $cnt1; ?>" id="TGKT_<?php echo $cnt1; ?>" value="<?php echo KT_formatDate($row_rskhuyenmai['TGKT']); ?>" size="10" maxlength="22" wdg:mondayfirst="false" wdg:subtype="Calendar" wdg:mask="<?php echo $KT_screen_date_format; ?>" wdg:type="widget" wdg:singleclick="false" wdg:restricttomask="no" wdg:readonly="true" />
              <?php echo $tNGs->displayFieldHint("TGKT");?> <?php echo $tNGs->displayFieldError("khuyenmai", "TGKT", $cnt1); ?> </td>
        </tr>
      </table>
      <input type="hidden" name="kt_pk_khuyenmai_<?php echo $cnt1; ?>" class="id_field" value="<?php echo KT_escapeAttribute($row_rskhuyenmai['kt_pk_khuyenmai']); ?>" />
      <?php } while ($row_rskhuyenmai = mysql_fetch_assoc($rskhuyenmai)); ?>
      <div class="KT_bottombuttons">
        <div>
          <?php 
      // Show IF Conditional region1
      if (@$_GET['idKM'] == "") {
      ?>
          <input type="submit" name="KT_Insert1" id="KT_Insert1" value="Thêm" />
          <?php 
      // else Conditional region1
      } else { ?>
          <div class="KT_operations">
            <input type="submit" name="KT_Insert1" value="Thêm mới" onclick="nxt_form_insertasnew(this, 'idKM')" />
          </div>
          <input type="submit" name="KT_Update1" value="Cập nhật" />
          <input type="submit" name="KT_Delete1" value="Xóa" onclick="return confirm('<?php echo NXT_getResource("Bạn có chắc muốn xóa ?"); ?>');" />
          <?php }
      // endif Conditional region1
      ?>
          <input type="button" name="KT_Cancel1" value="Hủy" onclick="return UNI_navigateCancel(event, '../includes/nxt/back.php')" />
        </div>
      </div>
    </form>
  </div>
  <br class="clearfixplain" />
</div>
<p>&nbsp;</p>

 </tr>
</table><?php include("admin_template/footer.php"); ?>  

</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset2);
?>

